<?php
/**
 * Created by IntelliJ IDEA.
 * User: XingHuo
 * Date: 15/4/7
 * Time: 下午9:58
 */

class Boot {
    static function  request($name,$default=''){
        return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
    }
    static function  get($arrary,$name,$default){
        return isset($arrary[$name]) ? $arrary[$name] : $default;
    }
    static function  link($route,$query=array()){
        return ''.$route.'?'.http_build_query($query);
    }
    static function getMongo($hostInfo=''){
        $str = "mongodb://" . $hostInfo;
        return $mg = new MongoClient($str);

    }

    static function parse(){
        $uri = self::get($_SERVER,'PATH_INFO','/admin/mongo');
        $uri = self::get($_SERVER,'REDIRECT_URL','/admin/mongo');
        $segment = explode('/',trim($uri,'/'));
        $ctrl = ucfirst("{$segment[1]}");
        $cls = ucfirst("{$segment[0]}"). $ctrl;
        $file = "{$segment[0]}/ctrl/{$cls}.php";
        $action = self::get($segment,2,'index');
        return array($file,$cls,$action);
    }
    static function run(){
        spl_autoload_register(array('Boot','autoload'));
        try {
            list($file,$cls,$action) = self::parse();
            include_once $file;
            $ctrl = new $cls;
            $ctrl->$action();
        } catch (Exception $e) {
            echo "404";
        }

        //call_user_func_array(array(new ))
    }
    static function output($data){
        echo json_encode($data);
    }
    static function view($file,$data=array()){
        extract($data);
        include_once APP_ROOT.'/admin/views'.$file;

    }
    static function autoload($class){
        $dirs = array(
            APP_PATH.'/helper/',
            APP_PATH.'/helper/',
        );
        foreach ($dirs as $dir) {
            $file  = $dir.$class.'.php';
//            echo $file;
//            echo $class;exit;
            if(file_exists($file)){
                  include $file;
                  break;
            }
        }
    }
    static  function config($group){
        return include_once APP_PATH.'/config/'.$group.'.php';
    }
}